package com.supor.aiot.mine.ui.recipe;


import android.view.View;

import com.android.baseconfig.base.RxBus3;
import com.android.baseconfig.common.utils.ToastUtils;
import com.android.baseconfig.common.views.AppBasicDialog;
import com.supor.aiot.common.app.Constants;
import com.supor.aiot.common.base.BaseMVVMActivity;
import com.supor.aiot.common.entity.Result;
import com.supor.aiot.mine.R;
import com.supor.aiot.mine.bean.RecipeApplyBean;
import com.supor.aiot.mine.databinding.ActivityRecipeBinding;

import io.reactivex.rxjava3.functions.Consumer;


public class RecipeCreateActivity extends BaseMVVMActivity<RecipeCreateViewModel, ActivityRecipeBinding> implements View.OnClickListener {

    private String applyId = null;
    private AppBasicDialog undoDialog;

    @Override
    protected int resourceId() {
        return R.layout.activity_recipe;
    }

    @Override
    protected void initView() {
        super.initView();
        bindingView.toolbar.setIvLeftOnClickListener(v -> finish());
        bindingView.toolbar.setTvLeftOnClickListener(v -> finish());
        bindingView.btnRecipeApply.setOnClickListener(this);
        bindingView.btnUndo.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        super.initData();
        showLoading();
        viewModel.getApplyStatus();

        viewModel.getApplyStatusResult().observe(this, result -> {
            closeLoading();
            if (result instanceof Result.Success) {
                int btnRecipeApplyVisible = View.VISIBLE;
                int llApplyingVisible = View.GONE;
                if (result.getData() != null && !("null".equalsIgnoreCase((String) result.getData()))) {
                    applyId = (String) result.getData();
                    btnRecipeApplyVisible = View.GONE;
                    llApplyingVisible = View.VISIBLE;
                }
                bindingView.btnRecipeApply.setVisibility(btnRecipeApplyVisible);
                bindingView.llApplying.setVisibility(llApplyingVisible);
            } else if (result instanceof Result.Error) {
                ToastUtils.getInstance().showCustomToast("获取食谱创作申请数据失败: " + ((Result.Error) result).getErrorMsg());
            }
        });

        viewModel.getApplyResult().observe(this, result -> {
            closeLoading();
            if (result instanceof Result.Success) {
                ToastUtils.getInstance().showCustomToast("申请食谱创作成功");
                RecipeApplyBean recipeApplyBean = (RecipeApplyBean) result.getData();
                viewModel.getApplyStatusResult().setValue(new Result.Success(recipeApplyBean.applyId));
            } else if (result instanceof Result.Error) {
                ToastUtils.getInstance().showCustomToast("申请食谱创作失败: " + ((Result.Error) result).getErrorMsg());
            }
        });

        viewModel.getUndoResult().observe(this, result -> {
            closeLoading();
            if (result instanceof Result.Success) {
                ToastUtils.getInstance().showCustomToast("撤销申请成功");
                viewModel.getApplyStatusResult().setValue(new Result.Success(null));
            } else if (result instanceof Result.Error) {
                ToastUtils.getInstance().showCustomToast("撤销申请失败: " + ((Result.Error) result).getErrorMsg());
            }
        });

        RxBus3.getInstance().register(Constants.Tag.LOGOUT, (Consumer<String>) s -> {
            if (Constants.Value.SUCCESS.equalsIgnoreCase(s)) {
                if (bindingView.llBtn.getVisibility() != View.GONE) {
                    bindingView.llBtn.setVisibility(View.GONE);
                }
            }
        }, this.getClass());
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.btn_recipe_apply) {
            showLoading();
            RecipeApplyBean recipeApplyBean = new RecipeApplyBean();
            viewModel.apply(recipeApplyBean);
        } else if (id == R.id.btn_undo) {
            showUndoDialog(getString(R.string.delete_user_title), getString(R.string.recipe_apply_undo_confirm));
        }
    }

    private void showUndoDialog(String title, String message) {
        if (undoDialog != null && undoDialog.isShowing()) {
            undoDialog.dismiss();
        }
        AppBasicDialog.Builder builder = new AppBasicDialog.Builder(this, false);
        builder.setTitle(title);
        builder.setMessage(message);
        builder.setPositiveButton(R.string.sure, (dialog, which) -> {
            showLoading();
            viewModel.undoApply(applyId);
            dialog.cancel();
        });
        builder.setNegativeButton(R.string.cancel, (dialog, which) -> dialog.cancel());
        undoDialog = builder.create();
        undoDialog.setCancelable(false);
        undoDialog.show();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        RxBus3.getInstance().unregister(this.getClass());
    }
}